<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Newscategory extends Controller {

    public function action_list() {
        Session::instance()->set('here', 'here');
        $name_template = '<a href="/admin/newstype/edit/{id}" title="点击修改">{name}</a> <a href="/admin/newstype/delete/{id}" class="delete_catalog" title="点击删除">X</a>';
        $conditional_name_template = '<a href="' . URL::base() . 'admin/newscategory/edit/{id}" title="点击修改">[ {position} ]-[ {name} ]</a> <a href="' . URL::base() . 'admin/newscategory/delete/{id}" class="delete_catalog" title="点击删除">X</a>';
        $catalog_tree = Catehelp::get_left_tree($name_template, $conditional_name_template, array('table' => 'news_categories', 'id' => 'id_category', 'name' => 'name'));
        //获得上级分类
        $news_categories = Newscategory::instances()->get_categories();
        //对模板文件变量赋值 
        $content_data = array('catalog_tree' => $catalog_tree, 'news_categories' => $news_categories);
        $view = Viewer::display('admin/content/newscategory_list', $content_data);
        $this->response->body($view);
    }

    public function action_add() {
        $_POST['addtime'] = time();
        $_POST['updatetime'] = time();
        $newcategory = Newscategory::instances()->set($_POST);
        if ($newcategory) {
            Message::set('分类添加成功！');
            Request::current()->redirect(URL::base() . 'admin/newscategory/list');
        }
        $view = Viewer::display('admin/content/newscategory_add');
        $this->response->body($view);
    }

    public function action_edit() {
        Session::instance()->set('here', 'here');
        $name_template = '<a href="/admin/newstype/edit/{id}" title="点击修改">{name}</a> <a href="/admin/newstype/delete/{id}" class="delete_catalog" title="点击删除">X</a>';
        $conditional_name_template = '<a href="' . URL::base() . 'admin/newscategory/edit/{id}" title="点击修改">[ {position} ]-[ {name} ]</a> <a href="' . URL::base() . 'admin/newscategory/delete/{id}" class="delete_catalog" title="点击删除">X</a>';
        $catalog_tree = Catehelp::get_left_tree($name_template, $conditional_name_template, array('table' => 'news_categories', 'id' => 'id_category', 'name' => 'name'));
        $id_category = $this->request->param('id');
        $category = ORM::factory('news_category')
                        ->where('id_category', '=', $id_category)
                        ->find()->as_array();
        if ($_POST) {
            $_POST['updatetime'] = time();
            $new = Newscategory::instances($id_category)->update($_POST);
            if ($new) {
                Message::set('新闻编辑成功！');
                Request::current()->redirect(URL::base() . 'admin/newscategory/edit/' . $id_category);
            }
        }
        $new_category = Newscategory::instances($id_category)->get();
        $news_categories = Newscategory::instances()->get_categories($category['id_parent']);
        $content_data = array('catalog_tree' => $catalog_tree, 'data' => $new_category, 'news_categories' => $news_categories);
        $view = Viewer::display('admin/content/newscategory_edit', $content_data);
        $this->response->body($view);
    }

    public function action_delete() {
        $id_category = $this->request->param('id');
        $flag = Newscategory::instances($id_category)->delete();
        if ($flag) {
            Message::set('分类删除成功');
            Request::current()->redirect(URL::base() . 'admin/newscategory/list');
        } else {
            Message::set('分类删除失败', 'error');
            Request::current()->redirect(URL::base() . 'admin/newscategory/list');
        }
    }

}